﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADNLib;

namespace ADNColorPicker
{
	public partial class ColorPickerDialog : Form
	{
        private ColorBlend lineColor = null;
        private ColorBlend fillColor = null;
        private LineOptions lineOpts = new LineOptions();
		public event EventHandler ColorChanged;

		public ColorPickerDialog()
		{
			InitializeComponent();
		}

        public ColorPickerDialog(ColorBlend lineColor, ColorBlend fillColor, LineOptions lineOpts)
        {
            InitializeComponent();

            if (lineColor != null) { this.lineColor = lineColor; }
            if (fillColor != null) { this.fillColor = fillColor; }
            if (lineOpts != null) { this.lineOpts = lineOpts; }
        }

        public ColorBlend LineColor
        {
            get { return lineColor; }
            set 
            { 
                lineColor = value;
                Refresh();
            }
        }

        public ColorBlend FillColor
        {
            get { return fillColor; }
            set
            { 
                fillColor = value;
                Refresh();
            }
        }

        public LineOptions LineSettings
        {
            get { return lineOpts; }
            set 
            { 
                lineOpts = value;
                UpdateLineSettings();
            }
        }

		public float Angle
		{
			get { return anglePreview.Angle; }
			set { anglePreview.Angle = value; }
		}

		private void OnColorChanged()
		{
			if (ColorChanged != null) { ColorChanged(this, EventArgs.Empty); }
		}

        private void colorBox1_ColorChanged(object sender, EventArgs e)
		{
			stripFill.SelectedColor = colorBox1.CurrentColor;
		}

		private void stripFill_ColorChanged(object sender, EventArgs e)
		{
			//fire form color changed event
			//set the color preview colors
			anglePreview.Gradient = stripFill.Gradient;
			if (rdoLineColor.Checked) { lineColor = stripFill.Gradient; }
			if (rdoFillColor.Checked) { fillColor = stripFill.Gradient; }
			OnColorChanged();
		}

		private void stripFill_SelectionChanged(object sender, EventArgs e)
		{
			colorBox1.CurrentColor = stripFill.SelectedColor;
		}

		private void rdoLineColor_CheckedChanged(object sender, EventArgs e)
		{
			if (rdoLineColor.Checked) { stripFill.Gradient = lineColor; }
		}

		private void rdoFillColor_CheckedChanged(object sender, EventArgs e)
		{
			if (rdoFillColor.Checked) { stripFill.Gradient = fillColor; }
		}

		private void btnReset_Click(object sender, EventArgs e)
		{
			if (rdoLineColor.Checked)
			{
				lineColor = new ColorBlend();
				lineColor.Colors = new Color[1] { Color.White };
				lineColor.Positions = new float[1] { 0.0f };

				stripFill.Gradient = lineColor;
			}

			if (rdoFillColor.Checked)
			{
				fillColor = new ColorBlend();
				fillColor.Colors = new Color[1] { Color.White };
				fillColor.Positions = new float[1] { 0.0f };

				stripFill.Gradient = fillColor;
			}

            OnColorChanged();
		}

        private void palBox_ColorChanged(object sender, EventArgs e)
        {
            stripFill.SelectedColor = palBox.SelectedColor;
            OnColorChanged();
        }

        private void UpdateLineSettings()
        {
            if (lineOpts != null)
            {
                spinLineWidth.Value = (decimal)lineOpts.Width;
            }
        }

        private void spinLineWidth_ValueChanged(object sender, EventArgs e)
        {
            if (lineOpts != null) 
            { 
                lineOpts.Width = (float)spinLineWidth.Value;
                OnColorChanged();
            }
        }

        private void cmboJoinType_SelectionChangeCommitted(object sender, EventArgs e)
        {

        }

        private void cmboStartCap_SelectionChangeCommitted(object sender, EventArgs e)
        {

        }

        private void cmboEndCap_SelectionChangeCommitted(object sender, EventArgs e)
        {

        }

        private void cmboDashes_SelectionChangeCommitted(object sender, EventArgs e)
        {

        }
	}


}
